﻿using DotNetExtensions.Core.Autofac;
using DotNetExtensions.Core.MVC;
using DotNetExtensions.Core.Restful;
using DotNetExtensions.Core.SqlSugar;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetExtensions.Core.SqlSugar
{
    [ApiController, Authorize]
    [Route("api/[controller]/[action]/{id?}")]
    public class BaseController<TModel, TSessoion, TSearchModel> : ControllerBase where TModel : class, new() where TSearchModel : SearchModel where TSessoion : class, new()
    {
        private readonly SimpleService<TModel, int, TSessoion, TSearchModel> simpleService;

        public SimpleService<TModel, int, TSessoion, TSearchModel> Service => simpleService;

        public readonly Session<TokenModel> Session;

        public BaseController(SimpleService<TModel, int,TSessoion, TSearchModel> simpleService)
        {
            Session = RegistTypeResolver.Default.Resolve<Session<TokenModel>>();
            this.simpleService = simpleService;
        }

        [HttpPost]
        public ApiResult<PageResult<TModel>> GetPageList([FromBody] QueryModel<TSearchModel> query)
        {
            return ApiResult.OK(simpleService.GetPageList(query));
        }

        [HttpPost]
        public ApiResult<List<TModel>> GetList([FromBody] TSearchModel query)
        {
            return ApiResult.OK(simpleService.GetList(query));
        }

        [HttpGet]
        public ApiResult<TModel> Get([FromQuery] int id)
        {
            return ApiResult.OK(simpleService.GetById(id));
        }

        [HttpGet]
        public ApiResult Delete([FromQuery] int id)
        {
            return ApiResult.OK(simpleService.Delete(id));
        }

        [HttpPost]
        public ApiResult Add([FromBody] TModel deviceModel)
        {
            bool ret = simpleService.Insert(deviceModel);

            return ret ? ApiResult.OK() : ApiResult.Failed();
        }

        [HttpPost]
        public ApiResult Update([FromBody] TModel deviceModel)
        {
            return ApiResult.OK(simpleService.Update(deviceModel));
        }
    }
}
